//
//  PKMineViewController.swift
//  Panku
//
//  Created by mac on 2017/2/6.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import YYKit
import HTHorizontalSelectionList
import SVProgressHUD

//Cell IDs
private let PKMineViewControllerListCellID = "PKMineViewControllerListCellID"

//headerView height
private let HeaderView_Height:CGFloat = 300.0 + 44

class PKMineViewController: PKBaseViewController ,UITableViewDelegate,UITableViewDataSource{

    
    //MARK: - properties
    lazy var headerView = UIView()
    lazy var avatarView = UIImageView()
    lazy var scrollView = UIScrollView()
    lazy var avatarBackgroundView = UIImageView()
    lazy var nameLabel = UILabel()
    lazy var idLabel = UILabel()
    lazy var editButton = PKBaseButton.createButton()
    
    lazy var selectionList = HTHorizontalSelectionList()
    
    //直播
    lazy var liveCountButton = PKBaseButton.createButton()
    //关注
    lazy var followCountButton = PKBaseButton.createButton()
    //粉丝
    lazy var fansCountButton = PKBaseButton.createButton()
    
    lazy var tableView = PKBaseTableView(frame: CGRect.zero, style: .grouped)
    
    
    //MARK: init
    init(viewModel:PKMineViewModel) {
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //viewModel
    var viewModel:PKMineViewModel?
    
    //MARK: - life circle
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    //MARK: - UI
    override func setupRootView() {
        
    }
    
    override func setupSubViews() {
        tableView.addSubview(scrollView)
        scrollView.addSubview(avatarBackgroundView)
        view.addSubview(tableView)
        headerView.addSubview(avatarView)
        headerView.addSubview(nameLabel)
        headerView.addSubview(idLabel)
        headerView.addSubview(liveCountButton)
        headerView.addSubview(followCountButton)
        headerView.addSubview(fansCountButton)
        headerView.addSubview(selectionList)
        headerView.addSubview(editButton)
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(PKMineViewControllerListCell.self, forCellReuseIdentifier: PKMineViewControllerListCellID)
        
        avatarView.image = R.image.mine_avatar()
        avatarView.layer.cornerRadius = 35
        avatarView.layer.masksToBounds = true
        avatarView.layer.borderWidth = 1
        avatarView.layer.borderColor = UIColor.white.cgColor
        headerView.frame = CGRect(x: 0, y: 0, width: APP_WIDTH, height: HeaderView_Height)
        headerView.backgroundColor = UIColor.clear
        
        nameLabel.textColor = UIColor(hexString: "343434")
        nameLabel.font = UIFont.PKFont(Size: 18)
        nameLabel.text = "金馆长大人"
        
        idLabel.font = UIFont.systemFont(ofSize: 16)
        idLabel.textColor = UIColor(hexString: "666666")
        idLabel.text = "ID:63092811"
        
        idLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(10)
            make.centerX.equalTo(nameLabel.snp.centerX)
        }
        
        editButton.setImage(R.image.mine_edit(), for: .normal)
        editButton.addBlock(for: .touchUpInside) { (_) in
            //修改昵称
        }
        
        liveCountButton.titleLabel?.font = UIFont.PKFont(Size: 14)
        liveCountButton.backgroundColor = UIColor.clear
        liveCountButton.setTitle("直播 5", for: .normal)
        liveCountButton.setTitleColor(UIColor(hexString: "323232"), for: .normal)
        liveCountButton.addBlock(for: .touchUpInside) { (_) in
            //TODO:打开直播
        }
        
        followCountButton.titleLabel?.font = UIFont.PKFont(Size: 14)
        followCountButton.backgroundColor = UIColor.clear
        followCountButton.setTitle("关注 32", for: .normal)
        followCountButton.setTitleColor(UIColor(hexString: "323232"), for: .normal)
        followCountButton.addBlock(for: .touchUpInside) { (_) in
            let followsController = PKFollowsViewController()
            self.navigationController?.pushViewController(followsController, animated: true)
            
        }
        
        fansCountButton.titleLabel?.font = UIFont.PKFont(Size: 14)
        fansCountButton.backgroundColor = UIColor.clear
        fansCountButton.setTitle("粉丝 1万", for: .normal)
        fansCountButton.setTitleColor(UIColor(hexString: "323232"), for: .normal)
        fansCountButton.addBlock(for: .touchUpInside) { (_) in
            //TODO:打开粉丝
        }
        
        //设置header
        tableView.tableHeaderView = headerView
        
        avatarBackgroundView.image = R.image.mine_logo()
        scrollView.backgroundColor = UIColor.black
        scrollView.frame = CGRect(x: 0, y: 0, width: APP_WIDTH, height: HeaderView_Height)
        scrollView.contentSize = CGSize(width: APP_WIDTH, height: HeaderView_Height)
        
        selectionList.frame = CGRect(x: 0, y: 0, width: 250, height: 44)
        selectionList.backgroundColor = UIColor.white
        selectionList.dataSource = self
        selectionList.delegate = self
        selectionList.centerButtons = true//居中
        selectionList.bottomTrimHidden = true//隐藏分割线
        selectionList.setTitleFont(UIFont.PKFont(Size: 16), for: .normal)
        
        selectionList.setTitleColor(UIColor(hexString: "666666")!, for: .normal)
        selectionList.setTitleColor(UIColor(hexString: "333333")!, for: .selected)
    }
    
    override func setupSubViewsConstraints() {
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        avatarView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headerView.snp.centerX)
            make.top.equalTo(headerView.snp.top).offset(40)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(avatarView.snp.bottom).offset(8)
            make.centerX.equalTo(avatarView.snp.centerX)
        }
        
        editButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.left.equalTo(nameLabel.snp.right).offset(15)
        }
        
        liveCountButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(idLabel.snp.bottom).offset(8)
        }
        
        followCountButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(liveCountButton.snp.centerY)
            make.right.equalTo(liveCountButton.snp.left).offset(-15)
        }
        
        fansCountButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(liveCountButton.snp.centerY)
            make.left.equalTo(liveCountButton.snp.right).offset(15)
        }
        
        selectionList.snp.makeConstraints { (make) in
            make.left.equalTo(headerView.snp.left)
            make.right.equalTo(headerView.snp.right)
            make.bottom.equalTo(headerView.snp.bottom)
            make.height.equalTo(44)
        }
        
        avatarBackgroundView.snp.makeConstraints { (make) in
            make.left.equalTo(headerView.snp.left)
            make.right.equalTo(headerView.snp.right)
            make.top.equalTo(headerView.snp.top)
            make.bottom.equalTo(selectionList.snp.top)  
        }

    }
    
    override func setupNavigationBar() {
//        //设置IM消息按钮
//        let IMButton = PKBaseButton.createButton()
//        IMButton.setImage(R.image.banner1(), for: .normal)
//        IMButton.addBlock(for: .touchUpInside) { (_) in
//            //TODO:弹出消息窗口
//        }
//        
//        let IMBarButtonItem = UIBarButtonItem(customView: IMButton)
//        self.navigationItem.rightBarButtonItem = IMBarButtonItem
        //隐藏navigationBar
        self.navigationController?.setNavigationBarHidden(true, animated: false)

    }
    
    override func bindViewModel() {
        
    }

}

extension PKMineViewController{
    
    func numberOfSections(in tableView: UITableView) -> Int {
//        return viewModel!.numberOfSections()
        return 8

    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return viewModel!.numberOfRows(InSection: section)
        return 1

    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PKMineViewControllerListCellID, for: indexPath)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let titles = ["我的收藏","我的发布","我的作品","买买买","卖卖卖","支付绑定","申请店家","设置"]
        (cell as! PKMineViewControllerListCell).titleLabel.text = titles[indexPath.section]
       
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        //动画deselect
        tableView.deselectRow(at: indexPath, animated: true)
        
        //弹出设置界面
        if indexPath.section == 7{
            let configController = PKConfigViewController(viewModel:viewModel!.viewModelForConfigController())
            self.navigationController?.pushViewController(configController, animated: true)
        }
        
        //M币贡献榜
        if indexPath.section == 0 && indexPath.row == 0{
            let contributionListController = PKMContributionListController()
            self.navigationController?.pushViewController(contributionListController, animated: true)
        }
        
        //账户
        if indexPath.section == 0 && indexPath.row == 3{
            let accountBalanceController = PKAccountBalanceController()
            self.navigationController?.pushViewController(accountBalanceController, animated: true)
        }
        
        if indexPath.section == 0 && indexPath.row == 2{
            //TODO:登录测试
            let loginController = PKLoginViewController(viewModel: viewModel!.viewModelForLoginViewController())
            self.navigationController?.pushViewController(loginController, animated: true)
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1.0
    }
    
}

//HTHorizontalSelectionList dataSource delegate
extension PKMineViewController:HTHorizontalSelectionListDelegate,HTHorizontalSelectionListDataSource{
    func numberOfItems(in selectionList: HTHorizontalSelectionList) -> Int {
        return 2
    }
    
    func selectionList(_ selectionList: HTHorizontalSelectionList, titleForItemWith index: Int) -> String? {
        let titles = ["个人中心","视频/商品"]
        return titles[index]
    }
    
    func selectionList(_ selectionList: HTHorizontalSelectionList, didSelectButtonWith index: Int) {
        //TODO:点击了titleView 的 tab
        SVProgressHUD.showInfo(withStatus: "点击了" + "\(index)")
    }
    
    
}
